class Connected:
    """
    两个或者三个宫位之间产生生克刑冲合害会关系
    """
    def __init__(self, relation, mingpan, cells):
        # 所有宫位都指向这个关系，确保每个关系只会被访问一次
        self.visited = False
        # 产生的某种关系
        self.relation = relation
        # 这个地方也可能产生第三种能量，比如：卯[阴木]戌[阳土]合化火
        # 产生的第三种能量直接加到命盘，如果摘除宫位，关系解除，命盘再减去对应增加的能量
        self.mingpan = mingpan
        # 相连的宫位
        self.cells = cells
        for cell in cells:
            cell.connections.append(self)

        self.energy_factors = {}
        self.energy = {}

    def inc_energy(self, wx, delta):
        self.energy[wx] = self.energy.get(wx, 0) + delta
        self.mingpan.inc_energy(wx, delta)

    def disconnect(self):
        for wx, e in self.energy.items():
            self.mingpan.inc_energy(wx, -e)
        self.energy = {}

    def __str__(self):
        from .dizhi import Zhi3He
        if isinstance(self.relation, Zhi3He):
            if len(self.cells) == 3:
                return str(self.relation)
            else:
                zhis = [cell.zhi.name for cell in self.cells]
                return self.relation.partial_str(zhis)
        else:
            return str(self.relation)
